<?php
/**
 * WebProduction Shop (wpshop)
 * @copyright (C) 2011-2012 WebProduction (tm) <webproduction.com.ua>
 *
 * This program is commercial software; you cannot redistribute it and/or
 * modify.
 */

/**
 * @author Egor Gerasimchuk <milhous@webproduction.ua>
 * @copyright WebProduction
 * @package Shop
 */
class Photoalbum extends XPhotoalbum {

    public function __construct($id = 0) {
        parent::__construct($id);
        $this->setClassname(__CLASS__);
    }

    /**
     * @return Photoalbum
     */
    public static function Get($key) {
        return self::GetObject("Photoalbum", $key);
    }

    /**
     * @return Photoalbum
     */
    public function getNext() {
        return parent::getNext();
    }

    /**
     * Создать массив фотографий альбома
     * @return array
     */
    public function makePhotoArray($cnt = false) {
        $photo = Triumph::Get()->getMediaService()->getPhotoByAlbumId($this->getId());
        if ($cnt) {
            $photo->setLimitCount($cnt);
        }
        $a = array();
        while ($x = $photo->getNext()) {
            $a[] = array(
                'id' => $x->getId(),
                'name' => $x->getName(),
                'image' => $x->makeImageThumb(150, 150, 'crop')
            );
        }
        return $a;
    }

    /**
     * @param int $width
     * @param bool $height
     * @param string $method
     * @return bool|mixed
     */
    public function makeImageThumb($width = 60, $height = 60, $method = 'prop') {
        $src = $this->getImage();
        $dir = MEDIA_PATH . 'photoalbum/';
        if (!$src || !file_exists($dir . $src)) {
            return;
        } else {
            $src = $dir . $src;
            PackageLoader::Get()->import('ImageProcessor');
            if ($method == 'prop') {
                $x = ImageProcessor_Thumber::MakeThumbProportional($src, $width, $height);
            } else {
                $x = ImageProcessor_Thumber::MakeThumbCrop($src, $width, $height);
            }
            $x = str_replace(PROJECT_PATH, '/', $x);
            return $x;
        }
    }

    /**
     * Получить информацию об альбоме, если передан параметр true, то вместе с фотографиями данного альбома
     * @param bool $photo
     * @return array
     */
    public function makeInfoArray($photo = false) {
        $a = array();
        $a['id'] = $this->getId();
        $a['name'] = $this->getName();
        $a['description'] = $this->getDescription();
        $a['cdate'] = $this->getCdate();
        $a['image'] = $this->makeImageThumb(300,300,'crop');
        $a['count'] = $this->getCount();
        $a['url'] = Engine::GetLinkMaker()->makeURLByContentIDParam('photo-view', $this->getId());
        if ($photo) {
            $a['photoArray'] = Triumph::Get()->getMediaService()->getPhotoByAlbumId($this->getId());
        }
        return $a;
    }
}
